package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.entity.TechnicianItemOrder;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.entity.UserInvite;
import com.rm.zeyou.mapper.DictConfigMapper;
import com.rm.zeyou.mapper.TechnicianItemOrderMapper;
import com.rm.zeyou.mapper.UserInviteMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.UserInviteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.util.QRcode.QRcodeUtil;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.response.Invitation;
import com.rm.zeyou.vo.response.SelectUserInviteTechnicianResponseVo;
import com.rm.zeyou.vo.response.SelectUserInviteUserResponseVo;
import com.rm.zeyou.vo.response.UserList;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户邀请表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 17:23:41
 */
@Service
public class UserInviteServiceImpl extends ServiceImpl<UserInviteMapper, UserInvite> implements UserInviteService {
    @Resource
    private UserInviteMapper userInviteMapper;
    @Resource
    private TechnicianItemOrderMapper technicianItemOrderMapper;
    @Resource
    private QRcodeUtil qRcodeUtil;

    @Resource
    private DictConfigMapper dictConfigMapper;
    @Value("${invite.inviteUrl}")
    public String inviteUrl;

    @Override
    public ApiRestResponse<SelectUserInviteUserResponseVo> selectUserInviteUser(Long userId) {
        SelectUserInviteUserResponseVo vo = new SelectUserInviteUserResponseVo();
        //邀请人数
        int inviteCount = userInviteMapper.selectCount(new LambdaQueryWrapper<UserInvite>()
                .eq(UserInvite::getInvitionUserId, userId)).intValue();
        vo.setInvitedCount(inviteCount);
        //未入账人数
        int notCreditedCount = userInviteMapper.selectCount(new LambdaQueryWrapper<UserInvite>()
                .eq(UserInvite::getInvitionUserId, userId)
                .eq(UserInvite::getInviteStatus, NumberConstants.ZERO_CONSTANTS)).intValue();
        vo.setNotCreditedCount(notCreditedCount);
        //已入账人数
        int creditedCount = userInviteMapper.selectCount(new LambdaQueryWrapper<UserInvite>()
                .eq(UserInvite::getInvitionUserId, userId)
                .eq(UserInvite::getInviteStatus, NumberConstants.ONE_CONSTANTS)).intValue();
        vo.setCreditedCount(creditedCount);
        //邀请榜单
        List<Invitation> invitationList = userInviteMapper.selectInvitationList();
        vo.setInvitationList(invitationList);
        List<UserList> inviteUserList = userInviteMapper.selectInviteUserList(userId);
        vo.setUserList(new PageInfo<>(inviteUserList));
        return ApiRestResponse.success(vo);
    }

    @Override
    public ApiRestResponse<SelectUserInviteTechnicianResponseVo> selectUserInviteTechnician(Long userId) {
        SelectUserInviteTechnicianResponseVo vo = new SelectUserInviteTechnicianResponseVo();
        //邀请榜单
        List<Invitation> invitationList = userInviteMapper.selectInvitationTechnicianList();
        vo.setInvitationList(invitationList);
        //邀请好友
        List<UserList> inviteUserList = userInviteMapper.selectInviteTechnicianList(userId);
        vo.setUserList(inviteUserList);
        //已邀请数量
        vo.setInviteTechnicianCount(inviteUserList.size());
        if (CheckEmptyUtil.isEmpty(inviteUserList)) {
            vo.setPendOrderCount(NumberConstants.ZERO_CONSTANTS);
            vo.setTakeOrderCount(NumberConstants.ZERO_CONSTANTS);
        } else {
            List<Long> tids = inviteUserList.stream().map(UserList::getTid).collect(Collectors.toList());
            //待接单
            int pendOrderCount = technicianItemOrderMapper.selectCount(new LambdaQueryWrapper<TechnicianItemOrder>()
                    .in(TechnicianItemOrder::getTid, tids)
                    .eq(TechnicianItemOrder::getOrderStatus, NumberConstants.ONE_CONSTANTS)).intValue();
            vo.setPendOrderCount(pendOrderCount);
            //已接单数量
            int takeOrderCount = technicianItemOrderMapper.selectCount(new LambdaQueryWrapper<TechnicianItemOrder>()
                    .in(TechnicianItemOrder::getTid, tids)
                    .in(TechnicianItemOrder::getOrderStatus, NumberConstants.TWO_CONSTANTS, NumberConstants.THREE_CONSTANTS, NumberConstants.FOUR_CONSTANTS)).intValue();
            vo.setTakeOrderCount(takeOrderCount);
        }
        return ApiRestResponse.success(vo);
    }

    @Override
    public ApiRestResponse<Map<String, String>> inviteUser(User user,Integer type) throws Exception {
        Map<String, String> returnMap = new HashMap<>();
        String qrCodeUrl = qRcodeUtil.getQRCodeImage(String.format(inviteUrl,user.getUserInvitationCode(),type), 300, 300);
        String inviteUserBg = dictConfigMapper.getInviteUserBg();
        returnMap.put("qrCodeUrl", qrCodeUrl);
        returnMap.put("inviteUserBg", inviteUserBg);
        returnMap.put("inviteUrl", String.format(inviteUrl,user.getUserInvitationCode(),type));
        return ApiRestResponse.success(returnMap);
    }

    @Override
    public ApiRestResponse<Map<String, String>> inviteTechnician(Technician technician, Integer type) throws IOException {
        Map<String, String> returnMap = new HashMap<>();
        String qrCodeUrl = qRcodeUtil.getQRCodeImage(String.format(inviteUrl,technician.getTechnicianInvitationCode(),type), 300, 300);
        String inviteUserBg = dictConfigMapper.getInviteUserBg();
        returnMap.put("qrCodeUrl", qrCodeUrl);
        returnMap.put("inviteUserBg", inviteUserBg);
        returnMap.put("inviteUrl", String.format(inviteUrl,technician.getTechnicianInvitationCode(),type));
        return ApiRestResponse.success(returnMap);
    }
}
